package com.jbm.enterapplication.mode

import android.content.Context
import android.content.Intent
import android.os.Environment
import android.util.Log
import com.jbm.core.client.MainCallback
import com.jbm.enterapplication.sharedprefer.SharedPreActivity
import com.jbm.enterapplication.utlisclass.HttpNet
import com.jbm.onehome.common.OHInterface
import rx.Observable
import rx.Observer

/**
 * 这是MyService 的Model
 * Created by JBMhaier on 2017/5/2.
 */

class MyServiceModel(private val context: Context, internal var callback: MainCallback, //    private static int mSockPort = 1920;

                     private val name: String) {
    private val sharedLoginActivity: SharedPreActivity
    /**
     * 正式服务器
     */
    /**/



    init {
        sharedLoginActivity = SharedPreActivity(context)
        if (sharedLoginActivity?.getIsConn() === false) {
            init()
        } else {
            if (name == "关机") {
                init()
            } else if (name == "开关") {
                connboo = true
                val `in` = Intent()
                `in`.action = "SOCKCONN"
                `in`.putExtra("comm", "3")
                context.sendBroadcast(`in`)
            } else {
                connboo = true
                val `in` = Intent()
                `in`.action = "SOCKCONN"
                `in`.putExtra("comm", "3")
                context.sendBroadcast(`in`)
            }

        }

    }

    private fun init() {
        HttpNet.getInstans().obsevice(Observable.create(Observable.OnSubscribe <Boolean>{ BooService->
            val strfile = Environment.getExternalStorageDirectory().toString() + "/Downs/"
            OHInterface.getG_server().m_temppath = strfile
           var booconn= HttpNet.getInstans().getOhInter().Init(null, mServerIp, mSockPort, mConnType, callback)//建立连接
            BooService.onNext(booconn)
        })).subscribe(object :Observer<Boolean>{
            override fun onCompleted() {
            }
            override fun onError(e: Throwable?) {
                val `in` = Intent()
                `in`.action = "SOCKCONN"
                `in`.putExtra("comm", "2")
                context.sendBroadcast(`in`)
                sharedLoginActivity.setIsConn(false)
                Log.e("main", "链接服务器失败" + e)
            }
            override fun onNext(t: Boolean?) {
                if (t!!) {
                    Log.e("main", "链接服务器成功")
                    val `in` = Intent()
                    `in`.action = "SOCKCONN"
                    `in`.putExtra("comm", "1")
                    context.sendBroadcast(`in`)
                    sharedLoginActivity.setIsConn(true)

                }
            }

        })

    }

    companion object {
        /**
         * 测试服务器
         */
        //    private static String mConnType = "app_mobile";
        //    private static String mConnType = "app_type";
        //    private  String mServerIp = "192.168.0.178";
        //    private  String mServerIp = "192.168.0.205";
        //    private  String mServerIp = "app.jiabangmang.com";
        private val mSockPort = 1920
        private val mConnType = "app_mobile"
        private val mServerIp = "192.168.0.16"
        var connboo = false
    }


}
